import {getOrder,payorder,confirm} from '../../utils/api'
Page({
  data: {
    currentTab: 5,
    orderList: [
      
    ]
  },

  // 切换订单状态标签
  switchTab(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      currentTab: index,
    });
    wx.showLoading({
      title: '加载中...', 
      mask: true, 
    });
    this.getOrderList(index);
  },

  // 获取订单列表
  async getOrderList(status) {
    
    const {data:res} = await getOrder(status)
    console.log(res)
    if(res.data.code != 200) return setTimeout(() => {
      wx.hideLoading();
      wx.showToast({
        title: '加载数据出错',
        icon: 'none',
        duration: 1000
      });
    }, 2000);
    res.data.data.map(item=>{
      item.url = JSON.parse(item.url)[0]
    })
    setTimeout(() => {
      wx.hideLoading();
      this.setData({
        orderList:res.data.data
     })
      wx.showToast({
        title: '加载完成',
        icon: 'success',
        duration: 1500
      });
    }, 500);
    
    
  },
  //去支付
  async pay(e){
    const {zong,oid} = e.currentTarget.dataset
    console.log(zong,oid)
    const obj = {
      zong,
      oid
    }
    const {data:res} = await payorder(obj)
    console.log(res)
    if(res.data.code !=200) return
    wx.showModal({
      title: '沙箱支付地址',
      content: '确认复制地址到剪贴板',
      showCancel: false,
      success: (restult) => {
        if (restult.confirm) {
          // 复制地址到剪贴板
          wx.setClipboardData({
            data: res.data.paymentUrl,
            success: () => {
              wx.showToast({
                title: '复制成功',
                icon: 'success',
                duration: 2000,
                mask:true
              });
              setTimeout(()=>{
                wx.navigateTo({
                  url: `/pages/payresult/payresult?id=${res.data.order}&&price=${zong}`,
                })
              },2000)
            },
            fail: () => {
              wx.showToast({
                title: '复制失败',
                icon: 'error',
                duration: 2000,
                mask:true
              });
            }
          });
        }
      }
    });
  },
  //查看物流
  viewlogistics(e){
    if(e.currentTarget.dataset.state == 0 ||e.currentTarget.dataset.state == 1) return
    wx.navigateTo({
      url: `/pages/logistics/logistics?id=${e.currentTarget.dataset.oid}`,
    })
  },
  //确认收货
  async confirmshop(e){
    // return console.log(e.currentTarget.dataset.oid)
    const {data:res} =await confirm(e.currentTarget.dataset.oid)
    console.log(res)
    if(res.data.code !=200)return wx.showToast({
      title: '收货失败',
      icon: 'none',
      duration: 2000,
      mask:true
    });
    this.getOrderList(this.data.currentTab)
    wx.showToast({
      title: '收货成功',
      icon: 'success',
      duration: 2000,
      mask:true
    });
    
  },
  onLoad() {
    this.getOrderList(5);
  }
});